#ifndef LINE_H
#define LINE_H

#include <QtGui>
#include "commons.h"

class Line : public QObject, public QGraphicsLineItem
{
    Q_OBJECT
public:
    explicit Line(Point* start,
                  QGraphicsItem *parent = 0,
                  QGraphicsScene *scene = 0,
                  qreal width = 4);
    Line(const Line& l);
    ~Line();
    void setStartPoint(Point* point);
    void setEndPoint(Point* point);
    void setDestnation(Point* point);
    void setDestnation(const QPoint& point);
    void setColor(const QColor& color);
    void update();

    void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

    inline Point *getStartPoint() {return startPoint;}
    inline Point *getEndPoint() {return endPoint;}
    QColor getColor();

    inline void setState(CanvasState state) {this->state=state;}
    inline CanvasState getState() const {return this->state;}

    void detatchStartPoint();
    void detatchEndPoint();
signals:
    void dragStart(Line* who);
    void dragStop(Line* who);
    void hoverEntered();
    void hoverLeft();
    void lineMoved(Line* who);
    void deleteLine(Line* line);

private:
    CanvasState state;
    CanvasState prevState;
    QGraphicsLineItem *left;
    QGraphicsLineItem *right;
    QGraphicsLineItem *top;
    QGraphicsLineItem *bottom;
    Point* startPoint;
    Point* endPoint;
    QPointF endDummyPoint;
    int PAD;
    DistanceF toStartPoint;
    DistanceF toEndPoint;

    void setBoundingRectVisibility(bool visible);
    void updateBoundingRect();
};

#endif // LINE_H
